using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonAttribute : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_CommonAttribute
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExImage icon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textTitle { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textValue { protected set; get; }
            public UnityEngine.GameObject change { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textChange { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage imageChange { protected set; get; }
            public UnityEngine.GameObject compare { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textCompare { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("icon", out var __tbv0);
                this.icon = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTitle", out var __tbv1);
                this.textTitle = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textValue", out var __tbv2);
                this.textValue = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("change", out var __tbv3);
                this.change = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textChange", out var __tbv4);
                this.textChange = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("imageChange", out var __tbv5);
                this.imageChange = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("compare", out var __tbv6);
                this.compare = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textCompare", out var __tbv7);
                this.textCompare = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private CfgAttribute cfgAttribute = null;

        protected UIB_UIN_CommonAttribute ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonAttribute();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.SetChangeActive(false);
            this.SetCompareActive(false);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this.cfgAttribute = null;
        }

        public void Show(int attrCid, int attrValue, int changeNum = 0)
        {
            this.Show(attrCid, attrValue, 0, 0);
            this.ShowChange(changeNum, attrValue);
        }

        public void ShowWhite(int attrCid, int attrValue, int changeNum = 0)
        {
            this.Show(attrCid, attrValue, 1, 1);
            this.ShowChange(changeNum, attrValue);
        }

        private void Show(int attrCid, int attrValue, int titleColorIndex, int valueColorIndex)
        {
            this.Show();

            CfgAttribute cfg = TableCenter.attribute.Get(attrCid);
            this.cfgAttribute = cfg;

            this.ui.textTitle.text = cfg.Name;
            //this.ui.textTitle.indexSwitchColor = titleColorIndex;
            this.ui.textValue.text = APIAttributeUIUtils.GetStrAttributeValue(this.cfgAttribute.ShowType, attrValue);
            //this.ui.textValue.indexSwitchColor = valueColorIndex;
            this.ui.textValue.SetActive(true);
            this.SetImageSprite(this.ui.icon, cfg.Icon_fullPath);
        }

        private void SetChangeActive(bool isActive)
        {
            this.ui.change.SetActive(isActive);
        }

        private void ShowChange(int changeNum, int attrValue)
        {
            bool isActive = changeNum != 0;
            this.ui.textValue.SetActive(!isActive);
            this.SetChangeActive(isActive);
            if (isActive)
            {
                this.ShowChangeNum(changeNum + attrValue);
                this.ShowChangeImage(changeNum + attrValue);
            }
        }

        private void ShowChangeNum(int changeNum)
        {
            bool isReduce = changeNum < 0;
            if (isReduce)
                changeNum = changeNum * -1;
            string strValue = APIAttributeUIUtils.GetStrAttributeValue(this.cfgAttribute.ShowType, changeNum);
            this.ui.textChange.text = $"{strValue}"; // isReduce ? $"-{strValue}" : $"+{strValue}";
            //this.ui.textChange.indexSwitchColor = isReduce ? 0 : 1;
        }

        private void ShowChangeImage(int changeNum)
        {
            bool isActive = changeNum != 0;
            bool isUp = changeNum > 0;
            this.SetChangeImageActive(isActive);
            if (isActive)
            {
                this.SetChangeImage(isUp);
            }
        }

        private void SetChangeImageActive(bool isActive)
        {
            this.ui.imageChange.SetActive(isActive);
        }

        private void SetChangeImage(bool isUp)
        {
            //this.ui.imageChange.indexSwitchSprite = isUp ? 1 : 0;
        }

        public void ShowCompare(int compareValue, int colorIndex = -1)
        {
            this.SetCompareActive(true);
            this.ui.textCompare.text = compareValue.ToString();
            this.SetCompareTextColor(colorIndex);
        }

        private void SetCompareActive(bool isActive)
        {
            this.ui.compare.SetActive(isActive);
        }

        private void SetCompareTextColor(int colorIndex)
        {
            if (colorIndex >= 0)
            {
                //this.ui.textCompare.indexSwitchColor = colorIndex;
            }
            else
            {
                this.ui.textCompare.color = this.ui.textValue.color;
            }
        }
    }
}
